package wang.hex.codetransfer;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import wang.hex.codetransfer.code.FileReceiver;
import wang.hex.codetransfer.code.QRCoderGen;
import wang.hex.codetransfer.code.Receiver;
import wang.hex.codetransfer.code.Sender;
import wang.hex.codetransfer.image.FileEncode;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Random;

@SpringBootApplication
public class CodeTransferApplication {

    public static void getI() {
        try {
            BufferedImage read = ImageIO.read(new File("1.jpg"));
            byte[] bytes = QRCoderGen.decodeBytes(read);
            System.out.println(new String(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void co1() throws Exception {
        Random random = new Random();
        while (true) {
            int len = random.nextInt(20);
            byte[] bytes = new byte[len];
            random.nextBytes(bytes);
            byte[] bytes1 = QRCoderGen.decodeBytes(QRCoderGen.encode(bytes, 600, 600));
            boolean b = Arrays.toString(bytes).equals(Arrays.toString(bytes1));
            if (!b) {
                System.out.println(Arrays.toString(bytes));
                System.out.println(Arrays.toString(bytes1));
                break;
            } else {
                System.out.println(1);
            }

        }
        //BufferedImage encode = QRCoderGen.encode(new byte[]{1,2,3,4,5,6,0,'v'}, 600, 600);

        // System.out.println( Arrays.toString( QRCoderGen.decodeBytes(encode)));
    }

    public static void co() {
        BufferedImage encode = QRCoderGen.encode("xasaasd", 600, 600);
        String s = QRCoderGen.decodeStr(encode);
        System.out.println(s);
    }

    public static void test1() throws Exception {
        Long start = System.currentTimeMillis();
        Sender sender = new Sender(0, 0, 600, 600);
        Receiver receiver = new Receiver(0, 0, 600, 600);
        sender.send(new FileInputStream("d:/bxsj_zd.csv"), (data) -> {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Receiver.ByteData byteData = receiver.doReceive();
            System.out.println((byteData.getPageNum() * 500 + 1 * 500) / (System.currentTimeMillis() - start) + " kb/s");
            System.out.println(byteData);
            return true;
        });

        //SpringApplication.run(CodeTransferApplication.class, args);
    }

    public static void main(String[] args) throws Exception {
        if (args.length == 0) {
            return;
        }
        String cmd = args[0];

        if (cmd.equals("EN")) {
            FileEncode.encodeFile(args[1], args[2]);
        } else if (cmd.equals("DE")) {
            FileEncode.decodeFile(args[1], args[2]);
        } else {
            int x = Integer.parseInt(args[1]);
            int y = Integer.parseInt(args[2]);
            int w = Integer.parseInt(args[3]);
            int h = Integer.parseInt(args[4]);
            int len = Integer.parseInt(args[5]);
            long delay = Long.parseLong(args[6]);
            String path = args[7];
            if (cmd.startsWith("R")) {


                FileReceiver receiver = new FileReceiver(x, y, w, h, len, delay);
                receiver.setFile(path);
                receiver.start();
                while (!receiver.getStoped()) {
                    Thread.sleep(1000L);
                }
            } else {
                Sender sender = new Sender(x, y, w, h, len);
                sender.send(path, delay);
            }

        }
    }

}
